package com.example.rubbish.DataBase;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.rubbish.gson.GarbageInfo;

import java.util.ArrayList;
import java.util.List;

public class GDataBase {
    public static  final String INFO_DB_NAME="Info.db";
    private static final int VERSION = 2;
    private static GDataBase GDataBase;
    private static SQLiteDatabase db;

    private GDataBase(Context context) {

        DataBaseHelper dataBaseHelper = new DataBaseHelper(context,INFO_DB_NAME,null, VERSION);
        db=dataBaseHelper.getWritableDatabase();
    }
    public synchronized static GDataBase getInstance(Context context) {
        if(GDataBase ==null){
            GDataBase =new GDataBase(context);
        }

        return GDataBase;
    }


    //保存GarbageInfo数据到数据库
    public  void saveGarbageInfo(GarbageInfo garbageInfo){
        if(garbageInfo!=null) {
            ContentValues values = new ContentValues();
            values.put("garbage_name",garbageInfo.getGarbageName());
            values.put("cate_name",garbageInfo.getCateName());
            values.put("ps",garbageInfo.getPs());
//            values.put("confidence",garbageInfo.getConfidence());
            db.insert("info",null,values);
        }
    }//saveGarbageInfo()

    public GarbageInfo getInfo(String name) {

        Cursor cursor = db.query("info",
                null,
                "garbage_name=?",
                new String[]{name},
                null,
                null,
                null);
       if(cursor.moveToFirst()){
           int i_garbage_name= cursor.getColumnIndex("garbage_name");
           int i_id=cursor.getColumnIndex( "id");
           int i_cate_name =cursor.getColumnIndex("cate_name");
           int i_ps= cursor.getColumnIndex("ps");
//           int i_confidence= cursor.getColumnIndex("confidence");
           GarbageInfo garbageInfo = new GarbageInfo();
           garbageInfo.setId(cursor.getInt(i_id));
           garbageInfo.setCateName(cursor.getString(i_cate_name));
           garbageInfo.setPs(cursor.getString(i_ps));
//           garbageInfo.setConfidence(cursor.getInt(i_confidence));
           garbageInfo.setGarbageName(cursor.getString(i_garbage_name));
           cursor.close();
           return  garbageInfo;
       }else {
           cursor.close();
           return  null;
       }

    }


    //从数据库中读取GarbageInfo数据
    public List<GarbageInfo> loadGarbageInfo(){
        List<GarbageInfo> list=new ArrayList<>();
        int i_id;
        int i_cate_name;
        int i_garbage_name;
        int i_ps;
//        int i_confidence;

        Cursor cursor = db.query("info",
                null,
                null,
                null,
                null,
                null,
                null);
        if(cursor.moveToFirst()){
            do{
                GarbageInfo garbageInfo = new GarbageInfo();
                i_id=cursor.getColumnIndex( "id");
                i_cate_name =cursor.getColumnIndex("cate_name");
                i_garbage_name= cursor.getColumnIndex("garbage_name");
                i_ps= cursor.getColumnIndex("ps");
//                i_confidence= cursor.getColumnIndex("confidence");

                garbageInfo.setId(cursor.getInt(i_id));
                garbageInfo.setCateName(cursor.getString(i_cate_name));
                garbageInfo.setGarbageName(cursor.getString(i_garbage_name));
                garbageInfo.setPs(cursor.getString(i_ps));
//                garbageInfo.setConfidence(cursor.getInt(i_confidence));
                ///add!!!!!!
                list.add(garbageInfo);

            }while(cursor.moveToNext());
        }
        cursor.close();
        return list;
    }




}
